package com.maxlen.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxlen.common.utils.PageUtils;
import com.maxlen.common.utils.Query;
import com.maxlen.common.utils.R;
import com.maxlen.common.vo.PageParamsVO;
import com.maxlen.ware.dao.WareInfoDao;
import com.maxlen.ware.entity.WareInfoEntity;
import com.maxlen.ware.entity.vo.FareVO;
import com.maxlen.ware.feign.MemberFeignService;
import com.maxlen.ware.service.WareInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Map;


@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    private MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(PageParamsVO params) {
        LambdaQueryWrapper<WareInfoEntity> wrapper = new LambdaQueryWrapper<>();

        String key = params.getKey();
        if(StringUtils.hasText(key)){
            wrapper.like(WareInfoEntity::getName, key);
        }

        IPage<WareInfoEntity> page = this.page(
                new Page<>(params.getPage(), params.getLimit()),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 根据地址 id 得到地址信息并计算运费
     * @param addressId
     * @return
     */
    @Override
    public R getFareByAddressId(Long addressId) {

        R addressByAddressId = memberFeignService.getAddressByAddressId(addressId);
        if(!addressByAddressId.isOk()){
            return R.error();
        }

        FareVO.MemberAddressVO data = addressByAddressId.getData(new TypeReference<FareVO.MemberAddressVO>() {});

        FareVO fareVO = new FareVO();
        fareVO.setAddressVO(data);

        //TODO 查询运费信息
        fareVO.setFare(new BigDecimal(10));

        return R.ok().setData(fareVO);
    }

}
